using System.Collections;
using UnityEngine;

public class FPSCamera : MonoBehaviour
{
	public static FPSCamera instance;

	private PlayerBehavior player;

	private float sensitivity = 12f;

	private float _sensitivity;

	[HideInInspector]
	public bool isMoveEnabled = true;

	public float mouseMoveLerp = 0.01f;

	public Camera FPSWeapon;

	[SerializeField]
	private Transform audioListener;

	public float minAng = -89.9f;

	public float maxAng = 89.9f;

	private Vector2 mouseDelta;

	private Vector2 mouseDeltaSumm;

	private Vector2 mouseDeltaLerp;

	public bool cameraShake;

	public float defaultShakeAmount = 0.3f;

	private float defaultShakeTime = 1f;

	private float currentShakeAmount;

	private float currentShakeTime;

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		SensitivityChanger.OnSensitivityChanged += UpdateSensitivity;
		UpdateSensitivity();
	}

	private void OnDestroy()
	{
		SensitivityChanger.OnSensitivityChanged -= UpdateSensitivity;
	}

	public void UpdateSensitivity()
	{
		float num = BCWStorage.Get<float>(settings.keySensivity);
		sensitivity = ((!(num > 0f)) ? 12f : num);
		_sensitivity = sensitivity;
	}

	public void Disable()
	{
		if (base.enabled)
		{
			if (player != null)
			{
				player.SwitchFPSCameraMode(false);
				player.FPSCamParent.localRotation = Quaternion.identity;
			}
			GetComponent<Camera>().fieldOfView = 60f;
		}
		_sensitivity = sensitivity * 1f;
		base.enabled = false;
		FPSWeapon.enabled = false;
	}

	public void Enable()
	{
		StartCoroutine(StartEnable());
	}

	public void OnEnterSniperMode()
	{
		_sensitivity = sensitivity * 0.2f;
	}

	public void OnExitSniperMode()
	{
		_sensitivity = sensitivity * 1f;
	}

	private IEnumerator StartEnable()
	{
		while (GameController.instance.playerScript == null || GameController.instance.playerScript.mecanimWrapper == null || MarketControllerUI.instance.MarketActive())
		{
			yield return null;
		}
		FPSWeapon.enabled = true;
		player = GameController.instance.playerScript;
		base.enabled = true;
		player.SwitchFPSCameraMode(true);
		_sensitivity = sensitivity * 1f;
		base.transform.position = player.fpsCamPivot.position;
		base.transform.rotation = player.fpsCamPivot.rotation;
		Camera cam = GetComponent<Camera>();
		cam.fieldOfView = 70f;
		cam.nearClipPlane = 0.01f;
	}

	private void LateUpdate()
	{
		player = PlayerBehavior.MyPlayer;
		if (player == null)
		{
			return;
		}
		mouseDeltaSumm += mouseDelta;
		Vector2 vector = mouseDeltaLerp;
		mouseDeltaLerp = mouseDeltaSumm;
		Vector2 vector2 = mouseDeltaLerp - vector;
		if (isMoveEnabled && vector2 != Vector2.zero && !GameController.instance.playerScript.onLadder)
		{
			Vector2 vector3 = vector2 * _sensitivity * 30f / Screen.width;
			player.transform.Rotate(Vector3.up, vector3.x);
			float num = player.FPSCamParent.eulerAngles.x;
			if (num > 180f)
			{
				num -= 360f;
			}
			else if (num < -180f)
			{
				num += 360f;
			}
			float value = 0f - vector3.y + num;
			value = Mathf.Clamp(value, minAng, maxAng);
			player.FPSCamParent.Rotate(Vector3.right, value - num);
		}
		base.transform.position = player.fpsCamPivot.position;
		if (player.inWater)
		{
			base.transform.rotation = Quaternion.Lerp(base.transform.rotation, player.fpsCamPivot.rotation, Time.deltaTime * 5f);
		}
		else
		{
			base.transform.rotation = player.fpsCamPivot.rotation;
		}
		ApplieCameraShake();
		audioListener.position = player.transform.position + Vector3.up * 2.25f;
	}

	public void Reset()
	{
		if ((bool)player)
		{
			player.FPSCamParent.rotation = Quaternion.Euler(0f, player.transform.rotation.eulerAngles.y, player.transform.rotation.eulerAngles.z);
		}
	}

	public void SetMouseDelta(Vector2 delta)
	{
		mouseDelta = delta;
	}

	public void ShakeCameraExplosion(Transform explosionTransform, float maxDistance)
	{
		float num = Vector3.Distance(explosionTransform.position, base.transform.position);
		if (num < maxDistance)
		{
			ShakeCamera((1f - num / maxDistance) / 4f, defaultShakeTime);
		}
	}

	public void ShakeCamera()
	{
		ShakeCamera(defaultShakeAmount, defaultShakeTime);
	}

	public void ShakeCamera(float shakeAmoun, float shakeTime)
	{
		cameraShake = true;
		currentShakeAmount = shakeAmoun;
		currentShakeTime = shakeTime;
	}

	private void ApplieCameraShake()
	{
		if (!cameraShake)
		{
			return;
		}
		if (currentShakeTime > 0f)
		{
			currentShakeTime -= Time.deltaTime;
			if (currentShakeAmount - Time.deltaTime / 3f > 0f)
			{
				currentShakeAmount -= Time.deltaTime / 3f;
			}
			base.transform.localPosition += Random.insideUnitSphere * currentShakeAmount;
		}
		else
		{
			currentShakeAmount = defaultShakeAmount;
			currentShakeTime = defaultShakeTime;
			cameraShake = false;
		}
	}
}
